package com.emapp.lccode.view

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.viewModelScope
import androidx.viewbinding.ViewBinding
import com.emapp.lccode.adapter.DialogTypeKtAdapter
import com.emapp.lccode.base.dismissLoadingExt
import com.emapp.lccode.bean.ProblemTypeKt
import com.emapp.lccode.bean.SelectType
import com.emapp.lccode.databinding.PopDistrictBinding
import com.emapp.lccode.mode.ProjectViewModel
import com.emapp.lccode.utils.vertical
import com.hjq.toast.Toaster
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.ext.util.loge
import me.hgj.jetpackmvvm.util.ActivityMessenger.finish

/**
 * 类型
 */
class ProjectDeviceTypeDialog : BaseDialog1() {
    private var mViewBind: PopDistrictBinding? = null
    private var deTypes1: ArrayList<ProblemTypeKt> = arrayListOf()
    private var deTypes2: ArrayList<ProblemTypeKt> = arrayListOf()
    private var deTypes3: ArrayList<ProblemTypeKt> = arrayListOf()
    private val adapter1: DialogTypeKtAdapter by lazy { DialogTypeKtAdapter() }
    private val adapter2: DialogTypeKtAdapter by lazy { DialogTypeKtAdapter() }
    private val adapter3: DialogTypeKtAdapter by lazy { DialogTypeKtAdapter() }
    private lateinit var myDialogViewModel: ProjectViewModel
    lateinit var selectType: SelectType
    var pid = 0
    var listener: SelectTypeListener? = null
    var hasInit = false
    fun show(activity: FragmentActivity) {
        show(activity.supportFragmentManager, null)
    }

    var project_id = ""

    override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): ViewBinding {
        return PopDistrictBinding.inflate(inflater, container, false).also { mViewBind = it }
    }

    override fun initView() {
        pid = 0
        myDialogViewModel =
            ViewModelProvider(this)[ProjectViewModel::class.java]
        mViewBind?.rvList0?.run {
            vertical()
            adapter = adapter1
        }
        adapter1.run {
            setOnItemClickListener { adapter, _, position ->
                val item = items[position]
                //   if (item.isCheck) return@setOnItemClickListener
                items.forEachIndexed { index, problemTypeKt ->
                    problemTypeKt.isCheck = index == position
                }
                notifyDataSetChanged()
                selectType.id0 = item.id
                selectType.name0 = item.title
                selectType.id1 = ""
                selectType.name1 = ""
                selectType.id2 = ""
                selectType.name2 = ""
                deTypes2.clear()
                deTypes3.clear()
                adapter3.submitList(deTypes3)
                adapter3.notifyDataSetChanged()
                item.child.forEach {
                    it.isCheck=false
                }
                deTypes2.addAll(item.child)
                adapter2.submitList(deTypes2)
                adapter2.notifyDataSetChanged()
            }
        }
        mViewBind?.rvList1?.run {
            vertical()
            adapter = adapter2
        }
        adapter2.run {
            setOnItemClickListener { adapter, _, position ->
                val item = adapter.items[position]
                // if (item.isCheck) return@setOnItemClickListener
                items.forEachIndexed { index, problemTypeKt ->
                    problemTypeKt.isCheck = index == position
                }
                notifyDataSetChanged()
                selectType.id1 = item.id
                selectType.name1 = item.title
                selectType.id2 = ""
                selectType.name2 = ""
                deTypes3.clear()
                item.child.forEach {
                    it.isCheck=false
                }
                deTypes3.addAll(item.child)
                adapter3.submitList(deTypes3)
                adapter3.notifyDataSetChanged()
            }
        }
        mViewBind?.rvList2?.run {
            vertical()
            adapter = adapter3
        }
        adapter3.run {
            setOnItemClickListener { adapter, _, position ->
                val item = adapter.items[position]
                if (item.isCheck) return@setOnItemClickListener
                items.forEachIndexed { index, problemTypeKt ->
                    problemTypeKt.isCheck = index == position
                }
                notifyDataSetChanged()
                selectType.id2 = item.id
                selectType.name2 = item.title
            }
        }
        myDialogViewModel.run {
            types.observe(viewLifecycleOwner) { resultState ->
                dismissLoadingExt()
                if (selectType.id0.isNotEmpty()) {
                    resultState.forEach { type ->
                        type.isCheck = type.id == selectType.id0
                        if (type.isCheck && selectType.id1.isNotEmpty()) {
                            type.child.forEach { child1 ->
                                child1.isCheck = child1.id == selectType.id1
                            }
                            adapter2.submitList(type.child)
                            adapter2.notifyDataSetChanged()
                        }
                    }
                }
               "数据："+ resultState.size.toString().loge()
                adapter1.submitList(resultState)
                adapter1.notifyDataSetChanged()
            }
        }
        getType(project_id)
        mViewBind?.ivCancel?.setOnClickListener {
            dismiss()
        }
        mViewBind?.tvOk?.setOnClickListener {
            //val listener = context as SelectTypeListener? ?: return@setOnClickListener
            if (selectType.id1.isBlank() && adapter2.itemCount > 0) {
                Toaster.show("请选择二级分类")
                return@setOnClickListener
            }
            listener?.onSelectTypeComplete(selectType)
            dismiss()
        }
    }

    fun getType(project_id: String) {
        myDialogViewModel.getDeviceType(project_id)
    }


    interface SelectTypeListener {
        fun onSelectTypeComplete(type: SelectType)
    }

}